/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.Color;

import javax.swing.JTextField;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.OperatingHours;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.gui.util.LabelPanel;
import org.aergia.vinny.gui.util.TextField;
import org.aergia.vinny.gui.view.EditorDialog.EditorPanel;
import org.aergia.vinny.util.DateUtil;

public class OperatingHoursEditor extends EditorPanel {

	private static final long serialVersionUID = 1L;

	private final VinnyController controller;
	private JTextField dayOfWeek;
	private JTextField endTime;
	private OperatingHours hours;
	private JTextField startTime;
	private final VinnyUser user;

	/**
	 * @param controller
	 * @param volunteer
	 */
	public OperatingHoursEditor(final VinnyController controller, final VinnyUser user, final OperatingHours hours) {
		this.controller = controller;
		this.user = user;
		this.hours = hours;
		this.initComponents();
	}

	@Override
	public String getTitle() {
		return "Operating Hours Editor";
	}

	@Override
	public boolean saveAction() {

		if (!this.verify()) {
			return false;
		}

		final int start = DateUtil.parseHour(this.startTime.getText());
		final int end = DateUtil.parseHour(this.endTime.getText());
		this.hours.setStartHour(start);
		this.hours.setEndHour(end);
		this.saveHours();
		return true;
	}

	private void initComponents() {

		this.dayOfWeek = new TextField(this.hours.getDayOfWeek().toString());
		this.dayOfWeek.setName("Day");
		this.dayOfWeek.setEditable(false);

		this.startTime = new TextField(DateUtil.formatHour(this.hours.getStartHour()));
		this.startTime.setName("Start Hour");
		this.startTime.setEditable(true);

		this.endTime = new TextField(DateUtil.formatHour(this.hours.getEndHour()));
		this.endTime.setName("End Hour");
		this.endTime.setEditable(true);

		this.add(new LabelPanel(FontUtil.getLabelFont(), Color.black, this.dayOfWeek, this.startTime, this.endTime));
		this.setVisible(true);
	}

	private void saveHours() {
		final ApiResult<OperatingHours> result = this.controller.saveHours(this.hours, this.user);
		if (!result.isError()) {
			this.hours = result.getResult();
		}
	}

	private boolean verify() {
		final int start = DateUtil.parseHour(this.startTime.getText());
		final int end = DateUtil.parseHour(this.endTime.getText());
		return start <= end;
	}
}
